import { isTrue } from 'pf-component';

export enum ServiceEnum {
  gateway = 'pf-gateway',
  system = 'pf-system',
  camunda = 'pf-camunda',
  auth = 'pf-auth'
}
export class BaseApi {
  private static readonly serviceUrl: {
    [key: string]: string;
  } = {
    // @ts-ignore
    [ServiceEnum.auth]: process.env.VUE_APP_AUTH_URL,
    // @ts-ignore
    [ServiceEnum.gateway]: process.env.VUE_APP_BASE_URL,
    // @ts-ignore
    [ServiceEnum.camunda]: process.env.VUE_APP_CAMUNDA_URL,
    // @ts-ignore
    [ServiceEnum.system]: process.env.VUE_APP_SYSTEM_URL
  };
  private static readonly prefix: string = process.env.VUE_APP_API_PREFIX || '';
  private static readonly permit = '/permitAll';
  private static readonly enableGateway: boolean =
    isTrue(Number(process.env.VUE_APP_ENABLE_GATEWAY)) || false; // 默认不开启网关
  private static readonly enableProxy: boolean = process.env.NODE_ENV === 'development';

  private static getBaseUrl(service: ServiceEnum): string {
    if (this.enableProxy) {
      // 开启代理
      if (this.enableGateway) {
        // 开启网关
        return `/${ServiceEnum.gateway}-api`;
      } else {
        // 不开启网关
        return `/${service}-api`;
      }
    } else {
      // 不开代理
      if (this.enableGateway) {
        // 开启网关
        return this.serviceUrl[ServiceEnum.gateway] || '';
      } else {
        // 不开启网关
        return this.serviceUrl[service];
      }
    }
  }
  static getUrl(service: ServiceEnum): string {
    return this.getBaseUrl(service) + '/' + service + this.prefix;
  }
  static getUrlNotPrefix(service: ServiceEnum): string {
    return this.getBaseUrl(service) + '/' + service;
  }
  static getUrlPermit(service: ServiceEnum): string {
    return this.getUrl(service) + this.permit;
  }
  static getAuthCodeHref(): string {
    return this.enableGateway
      ? `${this.serviceUrl[ServiceEnum.gateway]}/${ServiceEnum.auth}`
      : `${this.serviceUrl[ServiceEnum.auth]}/${ServiceEnum.auth}`;
  }
}
